package com.automobile.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.automobile.dao.UserDao;
import com.automobile.domain.User;
import com.automobile.service.UserService;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 用户模块
 */
@CacheConfig(cacheManager = "redisCacheManager",cacheNames = {"user"})
@Service("userService")
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserDao userDao;


    @Autowired
    JavaMailSenderImpl mailSender;



    /*
	 * 注册
	 */
	@Async
	public boolean registerUser(User user) {
		int flag1 = userDao.findUserByPhone(user.getPhone());
		int flag2 = userDao.findUserByEmail(user.getEmail());
		if(flag1 == 1||flag2 == 1) {
			return false;
		}
		user.setStatus("N");
		user.setCode(UUID.randomUUID().toString().replace("-", ""));
		userDao.save(user);
		String emailMsg = "恭喜您注册成功,请点击下面的链接进行激活 : "
				+ "http://134.175.87.59:8080/Smart/user/activate?activateCode="+user.getCode();

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(mimeMessage, true);
            helper.setSubject("《有个名字叫智能汽车》的激活验证码");
            helper.setText(emailMsg);
            helper.setTo(user.getEmail());
            helper.setFrom("18924203339@163.com");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        mailSender.send(mimeMessage);

        return true;
	}

	//登录
	public User loginUser(String username) {
		User resultUser = null;
		if(username.indexOf("@")==-1) {//是手机号
			try {
				resultUser = userDao.findUserByPhoneAsPassword(username);
			} catch (Exception e) {}
		}else {
			try {
				resultUser = userDao.findUserByEmailAsPassword(username);
			} catch (Exception e) {}
		}
		return resultUser;
	}
	
	//激活
	public boolean activate(String activateCode) {
		User user = null;
		try {
			user = userDao.findByActivateUser(activateCode);
		} catch (Exception e) {}
		if(user == null) {
			return false;
		}
		System.out.println("user:"+user);
		user.setStatus("Y");
		user.setCode("");
		userDao.updateActivate(user);
		return true;
	}
	
	/*
	 * 更新用户信息
	 */
	@CachePut(key = "#result.id")
	public User updateMessage(User user) {
		userDao.updateMessage(user);
		return user;
	}

	
	
	
	/*
	 * 更新密码
	 */
	@CachePut(key = "#result.id")
	public User updatePassword(User user) {
		userDao.updatePassword(user.getPhone(), user.getPassword());
		return user;
	}

	/*
	 * 更新手机号
	 */
    @CachePut(key = "#result.id")
	public User updatePhone(User user) {
		userDao.updatePhone(user.getId(), user.getPhone());
		return user;
	}

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    /*
	 * 获取用户的信息
	 */
    @Cacheable(key = "#userId")
	public User findUserById(int userId) {
		User user = null;
		try {
			user = userDao.findUserById(userId);
		} catch (Exception e) {}
		return user;
	}

}
